package g4p.styledtext;

/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski     
ISBN: 1-893115-78-X
Publisher: APress
 */

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

public class StyledSample {

	public static void main(String args[]) {
		String BOLD_ITALIC = "BoldItalic";
		String GRAY_PLAIN = "Gray";
		JFrame frame = new JFrame("Simple Attributes");
		Container content = frame.getContentPane();

		StyledDocument document = new DefaultStyledDocument();
		JTextPane textPane = new JTextPane(document);
		textPane.setEditable(true);
		JScrollPane scrollPane = new JScrollPane(textPane);
		content.add(scrollPane, BorderLayout.CENTER);

		Style style = (Style) document.getStyle(StyleContext.DEFAULT_STYLE);
		StyleConstants.setBold(style, true);
		StyleConstants.setItalic(style, true);
		StyleConstants.setForeground(style, Color.BLACK);
		document.addStyle(BOLD_ITALIC, null);


		// Insert content
		try {
			document.insertString(document.getLength(), "Hello Java\n", style);
		} catch (BadLocationException badLocationException) {
			System.err.println("Oops");
		}

		style = document.getStyle(StyleContext.DEFAULT_STYLE);
		StyleConstants.setBold(style, false);
		StyleConstants.setItalic(style, false);
		StyleConstants.setForeground(style, Color.PINK);
		document.addStyle(GRAY_PLAIN, null);

		style = document.getStyle(GRAY_PLAIN);

		// Insert content
		try {
			document.insertString(document.getLength(),
					" - Good-bye Visual Basic\n", style);
		} catch (BadLocationException badLocationException) {
			System.err.println("Oops");
		}


		frame.setSize(300, 150);
		frame.setVisible(true);
	}
}
